<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

class DemandRepository
{

    /**
     * 获得统计的列表
     * @param $type
     * @param $page
     * @param $perPage
     * @return mixed
     */
    public function getDemandStatisticList($type, $page, $perPage)
    {

        $list = app('model_room_demand_statistic')->with(['user_room' => function($query) {

            $query->select('id','room_name','user_id','room_id')->withOnly('user_data',['user_id','user_code']);

        }])->select(DB::raw('sum(order_num) as order_num,sum(demand_num) as demand_num,sum(receive_num) as receive_num,sum(valid_time) as valid_time'),'room_id','day_time','week_time','month_time');

        if($type == 2) {
            //按周分组
            $list = $list->groupBy('week_time');

        } elseif($type == 3) {
            //按月分组
            $list = $list->groupBy('month_time');

        } else {
            //按日分组
        }

        $list = $list->orderBy('day_time','desc')->paginate($perPage, ['*'], '', $page);

        foreach($list as &$val)
        {

            $val->user_code = isset($val->user_room->user_data->user_code) ? $val->user_room->user_data->user_code : 0;
//            $val->user_id = isset($val->user_room->user_data->user_id) ? $val->user_room->user_data->user_id : 0;
            $val->room_name = isset($val->user_room->room_name) ? $val->user_room->room_name : '';
//            unset($val->room_id);
            $val->room_code = isset($val->user_room->room_id) ? $val->user_room->room_id : 0;
            unset($val->user_room);
            //平均结单时间
            $val->avg_time = !empty($val->receive_num) ? floor($val->valid_time / $val->receive_num) : 0;
            unset($val->valid_time);
            //统计时间
            if($type == 1) {
                $val->time = date('Y-m-d',$val->day_time);
                $val->time_num = $val->day_time;
            } elseif($type == 2) {
                $val->time = date('Y-m-d',$val->week_time) .' to '. date('Y-m-d',$val->week_time+604800);
                $val->time_num = $val->week_time;
            } else {

                $val->time = 0;
                $val->time_num = $val->month_time;

            }
            unset($val->day_time);
            unset($val->week_time);
            unset($val->month_time);

        }

        return $list;

    }


    /**
     * 获得派单列表
     * @param $type
     * @param $room_id
     * @param $time
     * @param $page
     * @param $perPage
     * @return mixed
     */
    public function getDemandList($type, $room_id, $time, $page, $perPage)
    {
        //确定统计时间
        $start_at = $time;
        if($type == 1) {
            //按日统计
            $end_at = $time + 86400;
        } elseif($type == 2) {
            //按周统计
            $end_at = $time + 604800;
        } else {
            //按月统计
            $end_at = $time + 0;
        }

        $list = app('model_room_demand')
            ->whereBetween('add_time',[$start_at,$end_at])
            ->where('room_id',$room_id)->with(['lisence_order' => function($query){

                $query->select(DB::raw('sum(order_num) as order_num'),'demand_id');

        }])->withOnly('user_data',['user_id','nickname','user_code'])
            ->select('id','user_id','add_time','finish_time','status')
            ->orderBy('add_time','desc')->paginate($perPage, ['*'], '', $page);

        foreach($list as &$val)
        {
            $val->time_num = !empty($val->status) ? ($val->finish_time - $val->add_time) : 0;
            $val->add_time = date('Y-m-d H:i:s',$val->add_time);
            $val->finish_time = date('Y-m-d H:i:s',$val->finish_time);
            $val->user_code = isset($val->user_data->user_code) ? $val->user_data->user_code : 0;
            $val->nickname = isset($val->user_data->nickname) ? $val->user_data->nickname : '';
            $val->order_num = isset($val->lisence_order->order_num) ? $val->lisence_order->order_num : 0;
            unset($val->lisence_order);
            unset($val->user_data);
            unset($val->user_id);
        }

        return $list;
    }


    /**
     * 获得派单下单详情
     * @param $id
     * @return mixed
     */
    public function getDemandDetail($id)
    {
        $list = app('model_lisence_order')->where('demand_id',$id)->withOnly('user_data',['user_id','user_code','nickname'])
            ->withOnly('u_data',['user_id','user_code','nickname'])->select('user_id','uid','order_num','status','add_time')
            ->orderBy('add_time','desc')->get();

        foreach($list as &$val)
        {
            $val->u_code = isset($val->u_data->user_code) ? $val->u_data->user_code : 0;
            $val->u_name = isset($val->u_data->nickname) ? $val->u_data->nickname : '';
            $val->user_name = isset($val->user_data->nickname) ? $val->user_data->nickname : '';
            $val->user_code = isset($val->user_data->user_code) ? $val->user_data->user_code : 0;
            $val->add_time = date('Y-m-d H:i:s',$val->add_time);
            unset($val->user_id);
            unset($val->uid);
            unset($val->user_data);
            unset($val->u_data);
        }

        return $list;
    }



}